var express = require('express');
var bodyParser = require('body-parser');
var db = require('./fengzhuang2.js')
var app = express();
var formidable = require('formidable'); //提供上传文件的模块
var fs=require('fs');   //提供更改名字模块
var path = require('path'); //提供获取后缀名的方法
var ObjectId = require('mongodb').ObjectID;
app.use(bodyParser.urlencoded({extended:false}))

app.use('/logpage',express.static('./src/logpage'));

app.use('/regpage',express.static('./src/regpage'));

app.use('/goodsdetail',express.static('./src/goodsdetail'));

app.use('/shopcar',express.static('./src/shopcar'));

app.use('/login',function(req,res){
    res.header("Access-Control-Allow-Origin","*");
    let name = {
    	username:req.body.username,
    	password:req.body.password,
    }
    db.find('user',name,function(err,result){
    	if(err) console.log(err);
    	if(result==""){
    		res.send('none');
    	}else{
    		res.send('success')
    	}
    })
})  
app.use('/register',function(req,res){
    res.header("Access-Control-Allow-Origin","*");
    let name = {
    	username:req.body.username,
    	password:req.body.password,
    }
    let shopcar = [
    	{
    		name:'iphone',
    		quantity:0
    	},
    	{
    		name:'xiaomi',
    		quantity:0
    	},
    	{
    		name:'sanxing',
    		quantity:0
    	},
    	{
    		name:'huawei',
    		quantity:0
    	},
    ]
    let data = {
    	username:req.body.username,
    	password:req.body.password,
    	shopcar:shopcar,
    	_id:ObjectId,
    	firstbuy:true,
    }
    db.find('user',name,function(err,result){
    	if(err){
    		console.log(err)
    	}
    	if(result==""){
    		db.insertOne('user',data,function(err,result){
    			if(err){
    				console.log(err)
    			}
    			res.send('success');
    		})
    	}else{
    		res.send('only')
    	}
    })
    
})  

app.use('/buy',function(req,res){
	res.header("Access-Control-Allow-Origin","*");
	var hwquantity = req.body.hwquantity
	var ipquantity = req.body.ipquantity
	var sxquantity = req.body.sxquantity
	var xmquantity = req.body.xmquantity
	let username = req.body.username
	let data = {
	    hwquantity:hwquantity,
	    ipquantity:ipquantity,
	    sxquantity:sxquantity,
	    xmquantity:xmquantity,
	    username:username
    }
    
    let ip_obj = {
    	name:'iphone',
    	quantity:ipquantity,
    	price:5000
    }
    let xm_obj = {
    	name:'xiaomi',
    	quantity:xmquantity,
    	price:2500
    }
    let sx_obj = {
    	name:'sanxing',
    	quantity:sxquantity,
    	price:4500
    }
    let hw_obj = {
    	name:'huawei',
    	quantity:hwquantity,
    	price:3000
    }
    let newshopcar = [ip_obj,xm_obj,sx_obj,hw_obj];
    let name = {
    	username:username
    }
    db.find('user',name,function(err,result){
    	if(err) console.log(err)
    	
    	let state = result[0].firstbuy;
    	
    	if(state===true){
    		db.updateMany(
	        "user",
	        {
	            username:username
	        },
	        {
	            $set:
	            { 
	            	shopcar:newshopcar,
	            	firstbuy:false
	            }     
	        },
	        function(err,result){   //改完之后做什么
	            if(err) {
	                console.log(err);
	            }
	            res.send('success')
	        }
    		);
    	}else{
    		let arr = result[0].shopcar
    		let ipq = arr[0].quantity;
	    	let xmq = arr[1].quantity;
	    	let sxq = arr[2].quantity;
	    	let hwq = arr[3].quantity
    		let huaweiq = parseInt(hwquantity) + parseInt(hwq) 
			let iphoneq = parseInt(ipquantity) + parseInt(ipq)
			let sanxingq = parseInt(sxquantity) + parseInt(sxq)
			let xiaomiq = parseInt(xmquantity) + parseInt(xmq)
			let ip_obj = {
		    	name:'iphone',
		    	quantity:iphoneq,
		    	price:5000
		    }
		    let xm_obj = {
		    	name:'xiaomi',
		    	quantity:xiaomiq,
		    	price:2500
		    }
		    let sx_obj = {
		    	name:'sanxing',
		    	quantity:sanxingq,
		    	price:4500
		    }
		    let hw_obj = {
		    	name:'huawei',
		    	quantity:huaweiq,
		    	price:3000
		    }
		    let newshopcar = [ip_obj,xm_obj,sx_obj,hw_obj];
		    db.updateMany(
	        "user",
	        {
	            username:username,
	            
	        },
	        {
	            $set:
	            { 
	            	shopcar:newshopcar
	            }     
	        },
	        function(err,result){   //改完之后做什么
	            if(err) {
	                console.log(err);
	            }
	            res.send('success')
	        }
    		);


    	}
    })
})

app.use('/xuanran',function(req,res){
	let username = req.body.username;
	let data = {
		username:username,
	}
	db.find('user',data,function(err,result){
		if(err) console.log(err);
		
		res.send(result)
	})
})

app.use('/jian',function(req,res){
	let name = req.body.name;
	let username = req.body.username;
	let data = {
		username:username,
	}
	db.find('user',data,function(err,result){
		if(err) console.log(err)
		let arr = result[0].shopcar
		let iphoneq = arr[0].quantity;
		let xiaomiq = arr[1].quantity;
		let sanxingq = arr[2].quantity;
		let huaweiq = arr[3].quantity;
		if(name=="iphone"&&iphoneq!==0){
			iphoneq--;
		}else if(name=="xiaomi"&&xiaomiq!==0){
			xiaomiq--;
		}else if(name=="sanxing"&&sanxingq!==0){
			sanxingq--;
		}else if(name=="huawei"&&huaweiq!==0){
			huaweiq--;
		}
			let ip_obj = {
		    	name:'iphone',
		    	quantity:iphoneq,
		    	price:5000
		    }
		    let xm_obj = {
		    	name:'xiaomi',
		    	quantity:xiaomiq,
		    	price:2500
		    }
		    let sx_obj = {
		    	name:'sanxing',
		    	quantity:sanxingq,
		    	price:4500
		    }
		    let hw_obj = {
		    	name:'huawei',
		    	quantity:huaweiq,
		    	price:3000
		    }	
		    let newshopcar = [ip_obj,xm_obj,sx_obj,hw_obj];
		    db.updateMany(
	        "user",
	        {
	            username:username,
	            
	        },
	        {
	            $set:
	            { 
	            	shopcar:newshopcar
	            }     
	        },
	        function(err,result){   //改完之后做什么
	            if(err) {
	                console.log(err);
	            }
	            let findname = {
	            	username:username
	            }
	            db.find('user',findname,function(err,result){
	            	if(err) console.log(err)
	            	console.log(result);
	            	res.send(result)
	            })
	        }
    		);
		    	
		
	})
})

app.use('/jia',function(req,res){
	let name = req.body.name;
	let username = req.body.username;
	let data = {
		username:username,
	}
	db.find('user',data,function(err,result){
		if(err) console.log(err)
		let arr = result[0].shopcar
		let iphoneq = arr[0].quantity;
		let xiaomiq = arr[1].quantity;
		let sanxingq = arr[2].quantity;
		let huaweiq = arr[3].quantity;
		if(name=="iphone"){
			iphoneq++;
		}else if(name=="xiaomi"){
			xiaomiq++;
		}else if(name=="sanxing"){
			sanxingq++;
		}else if(name=="huawei"){
			huaweiq++;
		}
			let ip_obj = {
		    	name:'iphone',
		    	quantity:iphoneq,
		    	price:5000
		    }
		    let xm_obj = {
		    	name:'xiaomi',
		    	quantity:xiaomiq,
		    	price:2500
		    }
		    let sx_obj = {
		    	name:'sanxing',
		    	quantity:sanxingq,
		    	price:4500
		    }
		    let hw_obj = {
		    	name:'huawei',
		    	quantity:huaweiq,
		    	price:3000
		    }	
		    let newshopcar = [ip_obj,xm_obj,sx_obj,hw_obj];
		    db.updateMany(
	        "user",
	        {
	            username:username,
	            
	        },
	        {
	            $set:
	            { 
	            	shopcar:newshopcar
	            }     
	        },
	        function(err,result){   //改完之后做什么
	            if(err) {
	                console.log(err);
	            }
	            let findname = {
	            	username:username
	            }
	            db.find('user',findname,function(err,result){
	            	if(err) console.log(err)
	            	res.send(result)
	            })
	        }
    		);
		    	
		
	})
})

app.use('/clear',function(req,res){
	let username = req.body.username;
	let data = {
		username:username
	}
	db.updateMany(
	        "user",
	        {
	            username:username,
	            
	        },
	        {
	            $set:
	            { 
	            	shopcar:[],
	            	firstbuy:true,
	            }     
	        },
	        function(err,result){   //改完之后做什么
	            if(err) {
	                console.log(err);
	            }
	            db.find('user',data,function(err,result){
	            	if(err) console.log(err)
	            	res.send(result)
	            })
	        }
    		);

})


app.listen(3000);

